package com.example.maze;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.Window;
import android.view.View;

public class MazeActivity extends Activity
                          implements GameFinishedListenerI{

   public int getStatusBarHeight() {
      Rect r = new Rect();
      Window w = this.getWindow();
      if(w==null) {android.util.Log.d("DebugMsg", "Window w is empty");}
      w.getDecorView().getWindowVisibleDisplayFrame(r);
      if(r.isEmpty()) {android.util.Log.d("DebugMsg", "Rect r is empty");}
      return r.top;
   }
   
  public int getTitleBarHeight() {
      int viewTop = this.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
      return (viewTop - this.getStatusBarHeight());
  }
   
   public Vertex[][] newVertex(int M,int N,Vertex[][] vs) {
      for(int i=0; i<M; i++) {
         for(int j=0; j<N; j++) {
            vs[i][j]= new Vertex(i,j);
         }
      }
      return vs;
   }
   public void init(int M,int N,int size) {
      // step1: To set the size of maze
      DataSet.MAZE_M=M;
      DataSet.MAZE_N=N;
      DataSet.CELL_SIZE=size;
      //[ step2: To creat the basic logic data
      Vertex[][] vs= new Vertex[M][N];
      DataSet.VERTEXES= newVertex(M,N,vs);
      //[ step3: To creat data structrue "Graphic"
      DataSet.GRAPH= Graph.newGraph(M,N,vs);
      //[ step4: To creat data structrue "Spanning Tree"
      SpanningTree.setData(M,N,vs,DataSet.GRAPH);       //<<<< should be move to the Server
      DataSet.SPANNING_TREE= SpanningTree.getTree();
//for(Edge e: DataSet.SPANNING_TREE) {
//   android.util.Log.d("DebugMsg", e.toString());
//}      
      //[ step5: To generator our maze
      Maze.setData(M,N,size,DataSet.SPANNING_TREE);
      DataSet.MAZE_CELLS= Maze.getMaze();
      //[ finally init data
      DataSet.initCellState();
      DataSet.initFlagsState();
      DataSet.initPosition();
   }  //[by 2013/5/26 改由 Server產生
   public void init(int M,int N,int size, long[] indexArr) {  
    // step1: To set the size of maze
    DataSet.MAZE_M=M;
    DataSet.MAZE_N=N;
    DataSet.CELL_SIZE=size;
    //[ step2: To creat the basic logic data
    Vertex[][] vs= new Vertex[M][N];
    DataSet.VERTEXES= newVertex(M,N,vs);
    //[ step3: To creat data structrue "Graphic"
    DataSet.GRAPH= Graph.newGraph(M,N,vs);
    //[ step4: To creat data structrue "Spanning Tree"
    DataSet.SPANNING_TREE= SpanningTree.recovery(DataSet.GRAPH, indexArr); 
    //[ step5: To generator our maze
    Maze.setData(M,N,size,DataSet.SPANNING_TREE);
    DataSet.MAZE_CELLS= Maze.getMaze();    
    //[ finally init data
    DataSet.initCellState();
    DataSet.initFlagsState();
    DataSet.initPosition();
   } 
   /*private long[] server(int M,int N) {  
      Vertex[][] vs= new Vertex[M][N];
      for(int i=0; i<M; i++) {
         for(int j=0; j<N; j++) {
            vs[i][j]= new Vertex(i,j);
         }
      }        
      SpanningTree.setData(M,N,vs,Graph.newGraph(M,N,vs));  
      ArrayList<Edge> st= SpanningTree.getTree();
//for(Edge e: st) {
//android.util.Log.d("DebugMsg", e.toString());
//}        
      long[] indexArr= new long[st.size()];
      for(int i=0; i<st.size(); i++) {
         indexArr[i]= st.get(i).index(M,N);
         //android.util.Log.d("DebugMsg", st.get(i).index()+",");
      } 
//for(Edge e: st) {
//android.util.Log.d("DebugMsg", e.toString()+e.index());
//}      
      return indexArr;
   }*/
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      //[ 取得設備的寬與高
      DisplayMetrics metrics = this.getResources().getDisplayMetrics();
      final int SCREEN_WIDTH = metrics.widthPixels, 
                SCREEN_HEIGHT= metrics.heightPixels;
      //[ 取最上排的系統列的高
      View mLayout= this.findViewById(R.id.view1);//>
      //final int SYS_BAR_SIZE= ;
      final int SYS_BAR_SIZE= 111;
      DataSet.sysBarHeight= SYS_BAR_SIZE;
      //android.util.Log.d("DebugMsg", getStatusBarHeight()+","+getTitleBarHeight());
      Intent mIntent= this.getIntent();
      int M= mIntent.getIntExtra("m", -1);
      int N= mIntent.getIntExtra("n", -1);
      int size; 
      
      /* 已限定螢幕為直向顯示 by 2013/6/13
       * if(SCREEN_HEIGHT>SCREEN_WIDTH) {
//D      android.util.Log.d("DebugMsg", "螢幕正打直");
         //[ 先算 size約略值
         size=(int)(SCREEN_HEIGHT-SYS_BAR_SIZE)/18;                   
      } else {
//D      android.util.Log.d("DebugMsg", "螢幕正打橫");
         size=(int)(SCREEN_HEIGHT-SYS_BAR_SIZE)/9;
      }
      
      DataSet.setVisualMNSize((int)(SCREEN_HEIGHT-SYS_BAR_SIZE)/size,
                              (int)(SCREEN_WIDTH)/size);
      */
      size= (int)(SCREEN_HEIGHT-SYS_BAR_SIZE)/18;    //: 直向固定顯示 18格
      DataSet.setVisualMNSize(18 , (int)(SCREEN_WIDTH)/size);
      
      //android.util.Log.d("DebugMsg", "H:"+SCREEN_HEIGHT+" W:"+SCREEN_WIDTH);
      //android.util.Log.d("DebugMsg", "size:"+size);
      //android.util.Log.d("DebugMsg", "visualRangeM:"+DataSet.visualRangeM+" visualRangeN:"+DataSet.visualRangeN);
      if(savedInstanceState != null) {
         DataSetObj mSavedData= savedInstanceState.getParcelable("mSavedData");
         DataSet.MAZE_M = mSavedData.MAZE_M; //: 之後 online會用到, 免得每次都向 server拿
         DataSet.MAZE_N = mSavedData.MAZE_N;
         //DataSet.CELL_SIZE = mSavedData.CELL_SIZE;
         DataSet.CELL_SIZE= size;         
         DataSet.VERTEXES = mSavedData.VERTEXES;
         DataSet.GRAPH = mSavedData.GRAPH;
         DataSet.SPANNING_TREE = mSavedData.SPANNING_TREE;
//         DataSet.myPosM = mSavedData.myPosM;
//         DataSet.myPosN = mSavedData.myPosN;
//         DataSet.fromM = mSavedData.fromM;
//         DataSet.fromN = mSavedData.fromN;
         DataSet.flags = mSavedData.flags;
         DataSet.resetMyPosition(mSavedData.myPosM, mSavedData.myPosN);
      } else {
//         server(M,N);
         init(M,N,size,mIntent.getLongArrayExtra("STEdges"));
//         init(M,N,size);
         DataSet.resetMyPosition(0,0);          
      }
      DataSet.screenWidth= SCREEN_WIDTH;
      DataSet.screenHeight= SCREEN_HEIGHT-SYS_BAR_SIZE;
      
      DataSet.addListener(this);
      
      GamePanel gp= new GamePanel(this);
      setContentView(gp);
      //DataSet.mazeInfo();
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
   
   @Override
   public void onSaveInstanceState(Bundle savedInstanceState){
      DataSetObj mSavedData= new DataSetObj();
      mSavedData.MAZE_M = DataSet.MAZE_M;
      mSavedData.MAZE_N = DataSet.MAZE_N;
      //mSavedData.CELL_SIZE = DataSet.CELL_SIZE; 不存, 因為每次都不同
      mSavedData.VERTEXES = DataSet.VERTEXES;
      mSavedData.GRAPH = DataSet.GRAPH;
      mSavedData.SPANNING_TREE = DataSet.SPANNING_TREE;
      mSavedData.myPosM = DataSet.myPosM;
      mSavedData.myPosN = DataSet.myPosN;
//      mSavedData.fromM = DataSet.fromM;
//      mSavedData.fromN = DataSet.fromN;
      mSavedData.flags = DataSet.flags;
      savedInstanceState.putParcelable("mSavedData",mSavedData);
      super.onSaveInstanceState(savedInstanceState);
   }
   /*@Override
   public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      
   }*/
   
   @Override
   public void reachExit() {
      Intent intent= new Intent(this, FinalActivity.class);
      this.startActivity(intent);
   }
   @Override
   public void timeOut() {
      // do something
   }
   
   // --------------------------------------------------
   //[ disable back button
   @Override
   public void onBackPressed() {}
}
